package health_system.health_system.dto;

import health_system.health_system.entity.Moment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MomentWithSharesDTO {
    private Long id;
    private Long userId;
    private String username;
    private String userAvatarUrl;
    private String content;
    private Long shareCount;
    private Moment.VisibilityType visibility;
    private List<MomentShareDTO> recentShares;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private Boolean isSharedByCurrentUser;
    
    public static MomentWithSharesDTO fromMomentAndShares(
            Moment moment, 
            Long shareCount,
            List<MomentShareDTO> recentShares,
            Long currentUserId) {
        
        if (moment == null) {
            return null;
        }
        
        MomentWithSharesDTO dto = new MomentWithSharesDTO();
        dto.setId(moment.getId());
        
        if (moment.getUser() != null) {
            dto.setUserId(moment.getUser().getId());
            dto.setUsername(moment.getUser().getUsername());
            dto.setUserAvatarUrl(moment.getUser().getAvatarUrl());
        }
        
        dto.setContent(moment.getContent());
        dto.setShareCount(shareCount);
        dto.setVisibility(moment.getVisibility());
        dto.setRecentShares(recentShares);
        dto.setCreatedAt(moment.getCreatedAt());
        dto.setUpdatedAt(moment.getUpdatedAt());
        
        // Check if the current user has shared this moment
        if (currentUserId != null && moment.getShares() != null) {
            dto.setIsSharedByCurrentUser(moment.getShares().stream()
                    .anyMatch(share -> share.getUser().getId().equals(currentUserId)));
        } else {
            dto.setIsSharedByCurrentUser(false);
        }
        
        return dto;
    }
} 